04. Module Types
Modules Types
Classpath vs. Modulepath
Before modules, building a Java project involved placing all project classes and jars on the CLASSPATH
. This is the path where the compiler looks for all your class files.
With modules, you place your modules at a different location, called the MODULEPATH
. This is the path where the compiler looks for all your modules.
Unnamed Module
ND079 JPND C3 L3 A05a Unnamed Modules V3
All Java applications compiled in Java 9+ use the module system. Even if they contain no modules and do not use a module descriptor, everything placed on the CLASSPATH
are placed into what is called the Unnamed Module.
In the below example, some modules are added to the MODULEPATH
and some JAR is added to the CLASSPATH
.
Automatic Modules
ND079 JPND C3 L3 A05b Automatic Modules
Unnamed Module cannot access content in Modules, because it cannot use the requires
statement. Named modules cannot access the Unnamed Module, because the unnamed module cannot be referenced by require
.
The solution to this problem is automatic modules. Automatic modules are created by placing non-modular jars on the **modulepath. **If your project has dependencies, they can be placed on the modulepath so that they become automatic modules which you can reference by name in your module descriptor. The default name of automatic modules is the name of the jar, but that name can be overridden by the Automatic-Module-Name
property in the JAR Manifest. You can then reference the module using requires
.
Module Access
Automatic modules can access everything in the unnamed module. Maven places transitive dependencies on the classpath, so they become part of the unnamed module. The direct dependencies are placed on the modulepath and become automatic modules. This allows your module to access the direct dependencies, but not the transitive dependencies.